home *** CD-ROM | disk | FTP | other *** search
/ Macworld Expo - Develope…Central & Net Innovations / Developer Central and Net Innovators (MacWorld Expo) (January 1999).iso / Developer Central / Bowers Development / Demo AppMaker / Examples / C⁄C++ OS8 / Temperature / MainWindow.cp < prev    next >
Encoding:
Text File  |  1998-09-06  |  6.6 KB  |  346 lines  |  [TEXT/CWIE]

  1. // MainWindow.cp
  2.  
  3. #include <Types.h>
  4. #include <Quickdraw.h>
  5. #include <Controls.h>
  6. #include <Dialogs.h>
  7. #include <Events.h>
  8. #include <Lists.h>
  9. #include <Menus.h>
  10. #include <Resources.h>
  11. #include <Sound.h>
  12. #include <TextEdit.h>
  13. #include <ToolUtils.h>
  14. #include <Appearance.h>
  15.  
  16. #include "Globals.h"
  17. #include "ResourceDefs.h"
  18. #include "DoScrap.h"
  19. #include "Miscellany.h"
  20. #include "Scrolling.h"
  21. #include "ControlUtils.h"
  22. #include "DDocData.h"
  23. #include "TemperatureEngine.h"
  24. #include "TemperatureDoc.h"
  25.  
  26. #include "MainWindow.h"
  27.  
  28.  
  29. //----------
  30. void    MainWindow::Create (
  31.     AMDoc*            inDoc,
  32.     DDocData*        inData)
  33. {
  34.     MainWindow*        winObj = new MainWindow;
  35.  
  36.     if (winObj != nil) {
  37.         winObj->Open (inDoc);
  38.         winObj->ConnectToData (inData);
  39.     }
  40. }
  41.  
  42. //----------
  43. MainWindow::MainWindow ()
  44. {
  45. }
  46.  
  47. //----------
  48. MainWindow::~MainWindow ()
  49. {
  50. }
  51.  
  52. //----------
  53. TemperatureEngine*    MainWindow::GetEngine ()
  54. {
  55.     return (TemperatureEngine*) mDoc->mEngine;
  56. }
  57.  
  58. //----------
  59. void    MainWindow::Open (
  60.     AMDoc*            inDoc)
  61. {
  62.     WindowPtr        window;
  63.     Handle            wftb;
  64.  
  65.     mDoc = inDoc;
  66.  
  67.     window = GetNewCWindow (WIND_MainWindow, nil, (WindowPtr) -1L);
  68.     if (mDoc->mEngine->GetFilename () [0] != 0) {
  69.         SetWTitle (window, mDoc->mEngine->GetFilename ());
  70.     }
  71.     mWindow = window;
  72.     ((TemperatureDoc*)mDoc)->mMainWindowPtr = window;
  73.  
  74.     SetWindowKind (window, 'AM');
  75.     SetWRefCon (window, (long) this);
  76.     SetPort (window);
  77.     SetInfo (window);
  78.  
  79.     wftb = ::GetResource ('Wftb', WIND_MainWindow);
  80.  
  81.     CreateRootControl (window, &mRootControl);
  82.  
  83.     vScroll = nil;
  84.     hScroll = nil;
  85.  
  86.  
  87.     mCentigradeLabel = GetNewControl (CNTL_Centigrade, window);
  88.     SetWindowItemFont (mCentigradeLabel, wftb, 1);
  89.     SetControlFromTEXT (mCentigradeLabel, TEXT_Centigrade);
  90.  
  91.     mEditCentigradeHandle = ::GetNewControl (CNTL_EditCentigrade, window);
  92.     SetWindowItemFont (mEditCentigradeHandle, wftb, 2);
  93.  
  94.     mFahrenheitLabel = GetNewControl (CNTL_Fahrenheit, window);
  95.     SetWindowItemFont (mFahrenheitLabel, wftb, 3);
  96.     SetControlFromTEXT (mFahrenheitLabel, TEXT_Fahrenheit);
  97.  
  98.     mEditFahrenheitHandle = ::GetNewControl (CNTL_EditFahrenheit, window);
  99.     SetWindowItemFont (mEditFahrenheitHandle, wftb, 4);
  100.  
  101.     mCentSliderHandle = ::GetNewControl (CNTL_CentSlider, window);
  102.     SetWindowItemFont (mCentSliderHandle, wftb, 5);
  103.  
  104.     mFahrBarHandle = ::GetNewControl (CNTL_FahrBar, window);
  105.     SetWindowItemFont (mFahrBarHandle, wftb, 6);
  106.  
  107.     AdvanceKeyboardFocus (window);
  108.  
  109.     ShowWindow (window);
  110. }
  111.  
  112. //----------
  113. void    MainWindow::Close ()
  114. {
  115.     mData->RemoveResponder (this);
  116.  
  117.     ((TemperatureDoc*)mDoc)->mMainWindowPtr = nil;
  118.     SetInfo (nil);
  119.     HideWindow (mWindow);
  120.     DisposeWindow (mWindow);
  121.  
  122.     delete this;
  123. }
  124.  
  125. //----------
  126. void    MainWindow::ConnectToData (
  127.     DDocData*        inData)
  128. {
  129.     mData = inData;
  130.     mData->AddResponder (this);
  131.  
  132.     SetControlTextValue (mEditCentigradeHandle, mData->GetCentigrade ());
  133.     SetControlTextValue (mEditFahrenheitHandle, mData->GetFahrenheit ());
  134.     SetControlValue (mCentSliderHandle, mData->GetCentigrade ());
  135.     SetControlValue (mFahrBarHandle, mData->GetFahrenheit ());
  136. }
  137.  
  138. //----------
  139. void    MainWindow::DataChanged (
  140.     long        inDataID)
  141. {
  142.     if (inDataID == idCentigrade) {
  143.         SetControlTextValue (mEditCentigradeHandle, mData->GetCentigrade ());
  144.     }
  145.     if (inDataID == idFahrenheit) {
  146.         SetControlTextValue (mEditFahrenheitHandle, mData->GetFahrenheit ());
  147.     }
  148.     if (inDataID == idCentigrade) {
  149.         SetControlValue (mCentSliderHandle, mData->GetCentigrade ());
  150.     }
  151.     if (inDataID == idFahrenheit) {
  152.         SetControlValue (mFahrBarHandle, mData->GetFahrenheit ());
  153.     }
  154. }
  155.  
  156. //----------
  157. void    MainWindow::Control (
  158.     ControlHandle        whichControl,
  159.     short                whichPart,
  160.     Point                where)
  161. {
  162.     Rect            bounds;
  163.     short            newValue;
  164.  
  165.     ExitCurField ();
  166.  
  167.     if (whichControl == mEditCentigradeHandle) {
  168.         HandleEditClick (mEditCentigradeHandle, where);
  169.     }
  170.     if (whichControl == mEditFahrenheitHandle) {
  171.         HandleEditClick (mEditFahrenheitHandle, where);
  172.     }
  173.     if (whichControl == mCentSliderHandle) {
  174.         HandleControlClick (mCentSliderHandle, where, curEvent.modifiers, nil);
  175.         mData->SetCentigrade (GetControlValue (mCentSliderHandle));
  176.     }
  177. }
  178.  
  179. //----------
  180. void    MainWindow::MouseIn (
  181.     Point        where,
  182.     short        modifiers)
  183. {
  184.     Rect        bounds;
  185.  
  186. }
  187.  
  188. //----------
  189. void    MainWindow::ExitCurField ()
  190. {
  191.     ControlHandle    focus;
  192.  
  193.     GetKeyboardFocus (mWindow, &focus);
  194.  
  195.     if (focus == nil) {
  196.         // nothing to exit
  197.  
  198.     } else if (focus == mEditCentigradeHandle) {
  199.         mData->SetCentigrade (GetControlTextValue (mEditCentigradeHandle));
  200.     } else if (focus == mEditFahrenheitHandle) {
  201.         mData->SetFahrenheit (GetControlTextValue (mEditFahrenheitHandle));
  202.     }
  203. }
  204.  
  205. //----------
  206. void    MainWindow::TypeIn (
  207.     char        ch)
  208. {
  209.     ControlHandle    focus;
  210.     SInt16            keyCode;
  211.  
  212.     GetKeyboardFocus (mWindow, &focus);
  213.  
  214.     if ((ch == charEnter)
  215.     ||  (ch == charReturn)) {
  216.         ExitCurField ();
  217.     } else if (ch == charEsc) {
  218.     } else if (ch == charTab) {
  219.         DoTab ((curEvent.modifiers & optionKey) != 0);
  220.     } else if (focus != nil) {
  221.         keyCode = (SInt16)(curEvent.message & keyCodeMask);
  222.         HandleControlKey (focus, keyCode, ch, (SInt16)curEvent.modifiers);
  223.         mDoc->mEngine->SetDirty ();
  224.     } else {
  225.         SysBeep (1);
  226.     }
  227. }
  228.  
  229. //----------
  230. void    MainWindow::Resize ()
  231. {
  232.     /* application-specific code to resize items in window */
  233. }
  234.  
  235. //----------
  236. void    MainWindow::Scroll (
  237.     short        newValue,
  238.     short        oldValue)
  239. {
  240.     /* application-specific code to scroll window */
  241.     if (gWhichScroll == vScroll) {
  242.     } else {    // horizontal
  243.     }
  244. }
  245.  
  246. //----------
  247. void    MainWindow::DoUndo ()
  248. {
  249. } // DoUndo
  250.  
  251. //----------
  252. void    MainWindow::DoCut ()
  253. {
  254.     TEHandle        curTE = GetCurTE ();
  255.  
  256.     if (curTE != nil) {
  257.         TECut (curTE);
  258.         mDoc->mEngine->SetDirty ();
  259.         scrapDirty = true;
  260.     }
  261. } // DoCut
  262.  
  263. //----------
  264. void    MainWindow::DoCopy ()
  265. {
  266.     TEHandle        curTE = GetCurTE ();
  267.  
  268.     if (curTE != nil) {
  269.         TECopy (curTE);
  270.         scrapDirty = true;
  271.     }
  272. } // DoCopy
  273.  
  274. //----------
  275. void    MainWindow::DoPaste ()
  276. {
  277.     TEHandle        curTE = GetCurTE ();
  278.  
  279.     if (curTE != nil) {
  280.         TEPaste (curTE);
  281.         mDoc->mEngine->SetDirty ();
  282.     }
  283. } // DoPaste
  284.  
  285. //----------
  286. void    MainWindow::DoClear ()
  287. {
  288.     TEHandle        curTE = GetCurTE ();
  289.  
  290.     if (curTE != nil) {
  291.         TEDelete (curTE);
  292.         mDoc->mEngine->SetDirty ();
  293.     }
  294. } // DoClear
  295.  
  296. //----------
  297. void    MainWindow::DoSelectAll ()
  298. {
  299.     TEHandle        curTE = GetCurTE ();
  300.  
  301.     if (curTE != nil) {
  302.         TESetSelect (0, 32767, curTE);
  303.     }
  304. } // DoSelectAll
  305.  
  306. //----------
  307. void    MainWindow::DoShowClipboard ()
  308. {
  309. } // DoShowClipboard
  310.  
  311. //----------
  312. Boolean        MainWindow::DoCommand (
  313.     long        inCommand)
  314. {
  315.     Boolean        result = true;
  316.  
  317.     switch (inCommand) {
  318.         case cmdUndo:
  319.                 DoUndo ();
  320.             break;
  321.         case cmdCut:
  322.                 DoCut ();
  323.             break;
  324.         case cmdCopy:
  325.                 DoCopy ();
  326.             break;
  327.         case cmdPaste:
  328.                 DoPaste ();
  329.             break;
  330.         case cmdClear:
  331.                 DoClear ();
  332.             break;
  333.         case cmdSelectAll:
  334.                 DoSelectAll ();
  335.             break;
  336.         case cmdShowClipboard:
  337.                 DoShowClipboard ();
  338.             break;
  339.  
  340.         default:
  341.                 result = false;
  342.     } // switch
  343.  
  344.     return result;
  345. }
  346.